package com.shou.lifecollege.framework.Security.handle;

import com.alibaba.fastjson.JSON;
import com.shou.lifecollege.framework.Common.constant.HttpStatus;
import com.shou.lifecollege.framework.Common.result.RequestResult;
import com.shou.lifecollege.framework.Common.util.ServletUtils;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @Author: luotian
 * @Date: 2022/11/24 17:27
 */
@Component
public class CustomExpiredSessionStrategyImpl implements SessionInformationExpiredStrategy, Serializable {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        int code= HttpStatus.CONFLICT;
        String msg="您的账号已经在其它设备登录,如果非本人操作，请立即修改密码！";
        HttpServletResponse response=event.getResponse();
        ServletUtils.renderString(response, JSON.toJSONString(RequestResult.error(code,msg)));

    }
}
